# 北京-上海
from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
# import keys
from selenium.webdriver.common.keys import Keys

wd = webdriver.Chrome()
wd.get("https://www.12306.cn/")
"""
cf=wd.find_element(By.ID,"fromStationText")
cf.send_keys("北京")
cf.send_keys(Keys.TAB)
dd=wd.find_element(By.ID,"toStationText")
dd.send_keys("上海")
dd.send_keys(Keys.ENTER)
"""

wd.find_element(By.ID, "fromStationText").click()
wd.find_element(By.XPATH, '//*[@id="ul_list1"]/li[1]').click()
wd.find_element(By.XPATH, '//*[@id="ul_list1"]/li[2]').click()

sj = wd.find_element(By.ID, "train_date")
sj.clear()
sj.send_keys("2021-12-07")
wd.find_element(By.ID, "search_one").click()
# 切换窗口
win = wd.window_handles
# wd.switch_to.window(win[-1])
#打开窗口标题
for i in win:
    if "kyfw" in wd.current_url:
        break
wd.switch_to.window(i)
# 关闭窗口
wd.find_element(By.XPATH, "//div[@id='defaultwarningAlert_id']/div[2]/div[2]").click()
